<?php
namespace App\Common\Cla\Com;

use App\Common as com;

/**
 * 整数生成绝对不重复的唯一ID类库
 * @package App\Common\Cla\Com
 */
class Hashids extends com\Cla\ClaBase {

	private $_hashids = null;

	/**
	 * Hashids constructor.
	 * @param null $salt
	 * @param int $minHashLength
	 * @param null $alphabet
	 */
	public function __construct( $salt = null, $minHashLength = 0, $alphabet = null ) {
		// ...
		$salt = $salt ?? com\Cla\Web::getHashidsSaltKey();
		$alphabet = $alphabet ?? 'abcdefghijklmnpqrstuvwxyz1234567890';
		// $alphabet = $alphabet ?? 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
		//
		$this->_hashids = new \Hashids\Hashids( $salt, $minHashLength, $alphabet );
	}

	/**
	 * 加密
	 * @param mixed ...$numbers
	 * @return string
	 */
	public function encode( ...$numbers ): string {
		return $this->_hashids->encode( ...$numbers );
	}

	/**
	 * 解密
	 * @param $hash
	 * @return array
	 */
	public function decode( $hash ): array{
		return $this->_hashids->decode( $hash );
	}
	
}
